package models

import (
    "log"
)

// User 结构体表示用户，增加了余额、优惠券数量和会员状态字段
type User struct {
    ID            int     `json:"id"`
    Username      string  `json:"username"`
    Password      string  `json:"password"`
    Phonenumber   string  `json:"phonenumber"`
    Email         string  `json:"email"`
    Balance       float64 `json:"balance"`      // 用户余额
    CouponCount   int     `json:"coupon_count"` // 优惠券数量
    IsMember      bool    `json:"is_member"`    // 是否是会员
}

// 获取所有用户
func GetUsers() ([]User, error) {
    rows, err := db.Query("SELECT id, username, password, phonenumber, email, balance, coupon_count, is_member FROM users")
    if err != nil {
        log.Println("Error querying users: ", err)
        return nil, err
    }
    defer rows.Close()

    var users []User
    for rows.Next() {
        var u User
        err := rows.Scan(&u.ID, &u.Username, &u.Password, &u.Phonenumber, &u.Email, &u.Balance, &u.CouponCount, &u.IsMember)
        if err != nil {
            log.Println("Error scanning user: ", err)
            return nil, err
        }
        users = append(users, u)
    }

    return users, nil
}
// 获取用户信息，包括余额、优惠券数量和会员状态
func GetUserByID(id int) (User, error) {
    var user User
    err := db.QueryRow("SELECT id, username, password, phonenumber, email, balance, coupon_count, is_member FROM users WHERE id = ?", id).
        Scan(&user.ID, &user.Username, &user.Password, &user.Phonenumber, &user.Email, &user.Balance, &user.CouponCount, &user.IsMember)
    if err != nil {
        return user, err
    }
    return user, nil
}


// 向用户表添加用户，包括余额、优惠券数量和会员状态
func AddUser(u User) (int64, error) {
    stmt, err := db.Prepare("INSERT INTO users(username, password, phonenumber, email, balance, coupon_count, is_member) VALUES(?, ?, ?, ?, ?, ?, ?)")
    if err != nil {
        return 0, err
    }
    defer stmt.Close()

    result, err := stmt.Exec(u.Username, u.Password, u.Phonenumber, u.Email, u.Balance, u.CouponCount, u.IsMember)
    if err != nil {
        return 0, err
    }

    return result.LastInsertId()
}


// 删除用户
func DeleteUser(id int) (int64, error) {
    stmt, err := db.Prepare("DELETE FROM users WHERE id = ?")
    if err != nil {
        return 0, err
    }
    defer stmt.Close()

    result, err := stmt.Exec(id)
    if err != nil {
        return 0, err
    }

    // 获取删除的行数
    rowsAffected, err := result.RowsAffected()
    if err != nil {
        return 0, err
    }

    return rowsAffected, nil
}

// 更新用户余额
func UpdateUserBalance(userID int, amount float64) error {
    stmt, err := db.Prepare("UPDATE users SET balance = balance + ? WHERE id = ?")
    if err != nil {
        return err
    }
    defer stmt.Close()

    _, err = stmt.Exec(amount, userID)
    return err
}
// 更新用户优惠券数量
func UpdateUserCouponCount(userID int, couponCount int) error {
    stmt, err := db.Prepare("UPDATE users SET coupon_count = ? WHERE id = ?")
    if err != nil {
        return err
    }
    defer stmt.Close()

    _, err = stmt.Exec(couponCount, userID)
    return err
}
// 更改用户的会员状态
func UpdateUserMembershipStatus(userID int, isMember bool) error {
    stmt, err := db.Prepare("UPDATE users SET is_member = ? WHERE id = ?")
    if err != nil {
        return err
    }
    defer stmt.Close()

    _, err = stmt.Exec(isMember, userID)
    return err
}

// 在 models 包中定义根据用户名获取用户的函数
func GetUserByUsername(username string) (User, error) {
    var user User
    err := db.QueryRow("SELECT id, username, password, phonenumber, email, balance, coupon_count, is_member FROM users WHERE username = ?", username).
        Scan(&user.ID, &user.Username, &user.Password, &user.Phonenumber, &user.Email, &user.Balance, &user.CouponCount, &user.IsMember)
    if err != nil {
        return user, err
    }
    return user, nil
}
// 在 models 包中定义根据邮箱获取用户的函数
func GetUserByEmail(email string) (User, error) {
    var user User
    err := db.QueryRow("SELECT id, username, password, phonenumber, email, balance, coupon_count, is_member FROM users WHERE email = ?", email).
        Scan(&user.ID, &user.Username, &user.Password, &user.Phonenumber, &user.Email, &user.Balance, &user.CouponCount, &user.IsMember)
    if err != nil {
        return user, err
    }
    return user, nil
}



